<?php

use Illuminate\Database\Eloquent\SoftDeletingTrait;

class Taxonomy extends Eloquent{

	use SoftDeletingTrait;

	public $timestamps   = true;
	protected $dates     = ['deleted_at'];
	protected $table     = 'taxonomy';
	protected $validator = null;

	public function saveData($data){
		$taxonomy = new Taxonomy;

		if(isset($data['id']) && $data['id'] > 0){
			$taxonomy = Taxonomy::firstOrNew(array('id' => $data['id']));
		}

		$taxonomy->taxonomy = $data['taxonomy'];
		$taxonomy->type     = $data['type'];
		$taxonomy->order    = $data['order'];
		$taxonomy->save();
		Cache::forget('taxonomy-' . $data['type'] . '-all-data');

		return $taxonomy;
	}

	public function validator($data){
		$rules = array(
			'taxonomy' => array('required', 'alpha_num_spaces'),
			'type'     => array('required', 'alpha_num_spaces'),
			'order'    => array('numeric'),
			);

		$messages = array(
			'taxonomy.required'         => trans($data['type'] . '.required'),
			'taxonomy.alpha_num_spaces' => trans($data['type'] . '.alpha_num_spaces'),
			);

		$this->validator = Validator::make($data, $rules, $messages);

		return $this->validator;
	}

	public function getErrorMessages(){
		$messages = $this->validator->messages();
		
		$result   = array();

		foreach($messages->all() as $m){
			$result[]['message'] = $m;
		}

		return $result;
	}
}
